/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.spa.modules.gen.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.common.api.vo.Result;
import com.spa.common.generate.config.DataSourceConfig;
import com.spa.modules.gen.entity.GenData;
import com.spa.modules.gen.mapper.GenDataMapper;

/**
 * 数据源表Service
 * 
 * @author ThinkGem
 * @version 2013-10-15
 */
@Service
@Transactional(readOnly = true)
public class GenDataService extends ServiceImpl<GenDataMapper, GenData> {

	@Autowired
	private GenDataMapper genDataMapper;

	/**
	 * 分页列表查询
	 *
	 * @param spaUser
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Result<Object> queryPageList(HttpServletRequest req, Integer pageNo, Integer pageSize) {
		Page<GenData> page = new Page<>(pageNo, pageSize);
		LambdaQueryWrapper<GenData> queryWrapper = new LambdaQueryWrapper<>();
		Page<GenData> articlePage = genDataMapper.selectPage(page, queryWrapper);
		List<GenData> records = articlePage.getRecords();
		return Result.resultOK(records);
	}

	/**
	 * 获取全部数据
	 *
	 * @param spaUser
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Result<Object> queryAllDbList() {
		LambdaQueryWrapper<GenData> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByAsc(GenData::getCreateTime);
		queryWrapper.select(GenData::getDbName, GenData::getId);
		List<GenData> records = genDataMapper.selectList(queryWrapper);
		return Result.resultOK(records);
	}

	/**
	 * 根据数据源id获取数据源下的所有表
	 * 
	 * @param dbId
	 * @return
	 */
	public Result<Object> getTableListByDbId(String dbId) {
		QueryWrapper<GenData> qw = new QueryWrapper<>();
        qw.eq("id", dbId);
        GenData genData = genDataMapper.selectOne(qw);
		DataSourceConfig dsc = new DataSourceConfig();
		// 数据源驱动
		dsc.setDriverName(genData.getDrive());
		// 数据源连接
		dsc.setUrl(genData.getDbUrl());
		// 填写自己的数据库账号
		dsc.setUsername(genData.getDbUsername());
		// 填写自己的数据库密码
		dsc.setPassword(genData.getDbPassword());
		List<String> list = dsc.getTableNames(genData.getCatalog());
		dsc.closeConnection();
		return Result.resultOK(list);
	}

	/**
	 * 添加
	 *
	 * @param testTabel
	 * @return
	 */
	@Transactional
	public Result<Object> insert(GenData genData) {
		genDataMapper.insert(genData);
		return Result.resultOK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param spaUser
	 * @return
	 */
	@Transactional
	public Result<Object> saveById(GenData genData) {
		genDataMapper.updateById(genData);
		return Result.resultOK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@Transactional
	public Result<Object> deleteById(String id) {
		genDataMapper.deleteById(id);
		return Result.resultOK("删除成功!");
	}

	/**
	 * 测试数据源链接是否有效
	 *
	 * @param id
	 * @return
	 */
	@Transactional
	public Result<Object> testGenDb(GenData genData) {
		DataSourceConfig dsc = new DataSourceConfig();
		// 数据源驱动
		dsc.setDriverName(genData.getDrive());
		// 数据源连接
		dsc.setUrl(genData.getDbUrl());
		// 填写自己的数据库账号
		dsc.setUsername(genData.getDbUsername());
		// 填写自己的数据库密码
		dsc.setPassword(genData.getDbPassword());
		// 验证是否连接成功
		if (dsc.testConnection()) {
			dsc.closeConnection();
			return Result.resultOK("连接成功!");
		} else {
			dsc.closeConnection();
			return Result.error("连接失败");
		}

	}

}
